//
//  WYCRealnameController.m
//  51信用卡
//
//  Created by 中企互联 on 2017/11/1.
//  Copyright © 2017年 中企互联. All rights reserved.
//

#import "WYCRealnameController.h"
#import "WYCPhotographController.h"
#import "WYCBindingIDController.h"
#import "WYCLoginController.h"


@interface WYCRealnameController ()
{
    /** regStateNumber 为判断参数。登录返回传值
     *1.身份证正面没有扫描
     *2.身份证反面没有扫描
     *3.银行卡没有扫描
     *4.手持身份证没有扫描
     *5.手持银行卡没有扫描
     */
    int regStateNumber;
    
}

/** 正面按钮*/
@property(nonatomic,strong)UIButton * idButton;

/** 身份证反面按钮*/
@property(nonatomic,strong)UIButton *idsideButton;
/** 银行卡按钮*/
@property(nonatomic,strong)UIButton *cardButton;

/** 手持身份证按钮*/
@property(nonatomic,strong)UIButton * scanningholdButton;

/** 手持身份证正面按钮*/
@property(nonatomic,strong)UIButton * holdIDButton;
/** 手持银行卡正面按钮*/
@property(nonatomic,strong)UIButton * holdCardButton;



/** 正面按钮*/
@property(nonatomic,strong)UILabel * idLabel;

/** 身份证反面按钮*/
@property(nonatomic,strong)UILabel *idsideLabel;
/** 银行卡按钮*/
@property(nonatomic,strong)UILabel *cardLabel;

/** 手持身份证正面按钮*/
@property(nonatomic,strong)UILabel * holdIDLabel;
/** 手持银行卡正面按钮*/
@property(nonatomic,strong)UILabel * holdCardLabel;
@end

@implementation WYCRealnameController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = WYCGlobalBg;
    
    int sunmer = [self.regState intValue];
    
    regStateNumber = sunmer;
    //setNav
//    [self setupNav];
    
    
    [self setupCreat];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(test) name:@"ID" object:nil];
    
    
    //2.身份证反面
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(idsizetest) name:@"idside" object:nil];
    
    
    //3.银行卡
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(cardtest) name:@"card" object:nil];
    
    
    //4.手持身份证
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(holdIDtest) name:@"holdID" object:nil];
    
    //5.手持银行卡
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(holdcardtest) name:@"holdcard" object:nil];
}
//身份证通知正面
- (void)test{
    
    self.idLabel.text = @"身份证正面(已认证)";
    regStateNumber = 2;
}
//身份证反面通知
- (void)idsizetest{
    
    self.idsideLabel.text = @"身份证反面(已认证)";
    regStateNumber = 3;
}
//银行卡
- (void)cardtest{
    self.cardLabel.text = @"银行卡正面(已认证)";
    regStateNumber = 4;
    
}

//手持身份证
- (void)holdIDtest{
    
    self.holdIDLabel.text = @"手持身份证正面(已认证)";
    regStateNumber = 5;
}

//手持银行卡
- (void)holdcardtest{
    
    self.holdCardLabel.text = @"手持银行卡正面(已认证)";
    regStateNumber = 6;
}
#pragma make -------->>界面布局
- (void)setupCreat{
    
    UIView * registerView = [[UIView alloc] init];
    registerView.frame = CGRectMake(0, 0, ScreenWidth, NavHeight);
    registerView.backgroundColor = WYCMain;
    [self.view addSubview:registerView];
    
    
    //不是Nav。所以自己写的View和label
    UILabel * NavRigsterLabel = [[UILabel alloc] init];
    NavRigsterLabel.frame = CGRectMake(ScreenWidth / 2 - 200 * px, 20 * px, 400 * px, 64);
    NavRigsterLabel.textColor = [UIColor whiteColor];
    NavRigsterLabel.textAlignment = NSTextAlignmentCenter;
    //    NavRigsterLabel.font = [UIFont systemFontOfSize:BigFont];
    NavRigsterLabel.font = [UIFont boldSystemFontOfSize:BigFont];
    NavRigsterLabel.textAlignment = NSTextAlignmentCenter;
    NavRigsterLabel.text = @"信息认证";
    [registerView addSubview:NavRigsterLabel];
    
    
    
    //返回按钮
    UIButton *  backbutton = [[UIButton alloc] init];
    [backbutton setImage:[UIImage imageNamed:@"Nav_back"] forState:UIControlStateNormal];
    [backbutton addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    backbutton.frame = CGRectMake(40 * px, NavRigsterLabel.height / 3 + 20 * px, 80 * px , 80 * px);
    [registerView addSubview:backbutton];
    
    /********************身份证***************/
    //身份证认证
    UILabel * IDrealLabel = [[UILabel alloc] init];
    IDrealLabel.frame = CGRectMake(40 * px, 64 + 40 * px, ScreenWidth, 60 * px);
    IDrealLabel.text = @"添加身份证认证";
    IDrealLabel.font = smallLight;
    IDrealLabel.textColor = WYCbigGrayColor;
    [self.view addSubview:IDrealLabel];
    
    
    
    //身份证北京View
    UIView * IDView = [[UIView alloc] init];
    IDView.frame = CGRectMake(0, CGRectGetMaxY(IDrealLabel.frame) + 20 * px, ScreenWidth, 500 * px);
    IDView.backgroundColor = wycwhiteColor;
    [self.view addSubview:IDView];
    
    //身份证正面
    UIButton * IDButton = [[UIButton alloc] init];
    IDButton.frame = CGRectMake(40 * px, 40 * px, ScreenWidth / 2 - 80 * px, IDView.height / 3 * 2);
    [IDButton addTarget:self action:@selector(IDButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [IDButton setImage:[UIImage imageNamed:@"ID"] forState:UIControlStateNormal];
    [IDView addSubview:IDButton];
    self.idButton = IDButton;
    
    //身份证正面
    UILabel * IDlabel = [[UILabel alloc] init];
    IDlabel.frame = CGRectMake(IDButton.x, CGRectGetMaxY(IDButton.frame) + 20 * px, ScreenWidth / 2 - 80 * px, 80 * px);
    IDlabel.text = @"身份证正面";
    IDlabel.font = forLight;
    IDlabel.textAlignment = NSTextAlignmentCenter;
    [IDView addSubview:IDlabel];
    self.idLabel = IDlabel;
    
    //身份证反面
    UIButton * sideIDButton = [[UIButton alloc] init];
    sideIDButton.frame = CGRectMake(CGRectGetMaxX(IDButton.frame) + 80 * px, IDButton.y, IDButton.width, IDButton.height);
    [sideIDButton addTarget:self action:@selector(sideButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [sideIDButton setImage:[UIImage imageNamed:@"side_ID"] forState:UIControlStateNormal];
    [IDView addSubview:sideIDButton];
    self.idsideButton = sideIDButton;
    
    
    //身份证反面
    UILabel * sideIDlabel = [[UILabel alloc] init];
    sideIDlabel.frame = CGRectMake(sideIDButton.x, IDlabel.y, IDlabel.width, IDlabel.height);
    sideIDlabel.text = @"身份证反面";
    sideIDlabel.font = forLight;
    sideIDlabel.textAlignment = NSTextAlignmentCenter;
    [IDView addSubview:sideIDlabel];
    self.idsideLabel = sideIDlabel;
    
    
    /*******************手持身份证**************/
    //手持身份证
    UILabel * IDholdLabel = [[UILabel alloc] init];
    IDholdLabel.frame = CGRectMake(IDrealLabel.x, CGRectGetMaxY(IDView.frame) + 20 * px, ScreenWidth, 60 * px);
    IDholdLabel.text = @"添加银行卡，手持身份证";
    IDholdLabel.font = smallLight;
    IDholdLabel.textColor = WYCbigGrayColor;
    [self.view addSubview:IDholdLabel];
    
    
    //手持身份证View
    UIView * IDholdView = [[UIView alloc] init];
    IDholdView.frame = CGRectMake(0, CGRectGetMaxY(IDholdLabel.frame) + 20 * px, ScreenWidth, 500 * px);
    IDholdView.backgroundColor = wycwhiteColor;
    [self.view addSubview:IDholdView];
    
    //手持身份证正面
    UIButton * cardButton = [[UIButton alloc] init];
    cardButton.frame = CGRectMake(40 * px, 40 * px, ScreenWidth / 2 - 80 * px, IDView.height / 3 * 2);
    [cardButton setImage:[UIImage imageNamed:@"register_card"] forState:UIControlStateNormal];
    [cardButton addTarget:self action:@selector(cardButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [IDholdView addSubview:cardButton];
    self.cardButton = cardButton;
    
    //手持身份证正面
    UILabel * cardlabel = [[UILabel alloc] init];
    cardlabel.frame = CGRectMake(cardButton.x, CGRectGetMaxY(cardButton.frame) + 20 * px, cardButton.width, 80 * px);
    cardlabel.text = @"银行卡正面";
    cardlabel.font = forLight;
    cardlabel.textAlignment = NSTextAlignmentCenter;
    [IDholdView addSubview:cardlabel];
    self.cardLabel = cardlabel;
    
    
    //手持身份证正面
    UIButton * holdIDButton = [[UIButton alloc] init];
    holdIDButton.frame = CGRectMake(CGRectGetMaxX(cardButton.frame) + 80 * px, cardButton.y, cardButton.width, cardButton.height);
    [holdIDButton addTarget:self action:@selector(holdIdButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [holdIDButton setImage:[UIImage imageNamed:@"Hold_ID"] forState:UIControlStateNormal];
    [IDholdView addSubview:holdIDButton];
    self.holdIDButton = holdIDButton;
    
    
    //手持身份证反面
    UILabel * holdIDlabel = [[UILabel alloc] init];
    holdIDlabel.frame = CGRectMake(sideIDButton.x, cardlabel.y, cardlabel.width, cardlabel.height);
    holdIDlabel.text = @"手持身份证正面";
    holdIDlabel.font = forLight;
    holdIDlabel.textAlignment = NSTextAlignmentCenter;
    [IDholdView addSubview:holdIDlabel];
    self.holdIDLabel = holdIDlabel;
    
    /*******************添加银行卡**************/
    //手持身份证
    UILabel * cardLabel = [[UILabel alloc] init];
    cardLabel.frame = CGRectMake(IDrealLabel.x, CGRectGetMaxY(IDholdView.frame) + 20 * px, ScreenWidth, 60 * px);
    cardLabel.text = @"添加手持银行卡";
    cardLabel.font = smallLight;
    cardLabel.textColor = WYCbigGrayColor;
    [self.view addSubview:cardLabel];
    
    
    //手持手持银行卡View
    UIView * cardView = [[UIView alloc] init];
    cardView.frame = CGRectMake(0, CGRectGetMaxY(cardLabel.frame) + 20 * px, ScreenWidth, 500 * px);
    cardView.backgroundColor = wycwhiteColor;
    [self.view addSubview:cardView];
    
    //手持手持银行卡
    UIButton * holdcardButton = [[UIButton alloc] init];
    holdcardButton.frame = CGRectMake(40 * px, 40 * px, ScreenWidth / 2 - 80 * px, IDView.height / 3 * 2);
    [holdcardButton addTarget:self action:@selector(holdcardButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [holdcardButton setImage:[UIImage imageNamed:@"组6"] forState:UIControlStateNormal];
    [cardView addSubview:holdcardButton];
    self.holdCardButton = holdcardButton;
    
    //手持身份证反面
    UILabel * holdcardlabel = [[UILabel alloc] init];
    holdcardlabel.frame = CGRectMake(cardButton.x, CGRectGetMaxY(holdcardButton.frame) + 20 * px, cardButton.width, 80 * px);
    holdcardlabel.text = @"手持银行卡正面";
    holdcardlabel.font = forLight;
    holdcardlabel.textAlignment = NSTextAlignmentCenter;
    [cardView addSubview:holdcardlabel];
    self.holdCardLabel = holdcardlabel;
    
    
    if (regStateNumber == 6) {
        //身份证
        self.idLabel.text = @"身份证正面(已认证)";
        self.idButton.enabled = NO;
         
        
        self.idsideLabel.text = @"身份证反面(已认证)";
        self.idsideButton.enabled = NO;
        
        
        self.cardLabel.text = @"银行卡正面(已认证)";
        self.cardButton.enabled = NO;
        
        
        self.holdIDLabel.text = @"手持身份证正面(已认证)";
        self.holdIDButton.enabled = NO;
        
        self.holdCardLabel.text = @"手持银行卡正面(已认证)";
        self.holdIDButton.enabled = NO;
        
        
        
    }else if (regStateNumber == 5){
        //身份证
        self.idLabel.text = @"身份证正面(已认证)";
        self.idButton.enabled = NO;
        
        
        self.idsideLabel.text = @"身份证反面(已认证)";
        self.idsideButton.enabled = NO;
        
        
        self.cardLabel.text = @"银行卡正面(已认证)";
        self.cardButton.enabled = NO;
        
        
        self.holdIDLabel.text = @"手持身份证正面(已认证)";
        self.holdIDButton.enabled = NO;
        
        self.holdCardLabel.text = @"手持银行卡正面";
        self.holdIDButton.enabled = YES;
        
    }else if (regStateNumber == 4){
        
        //身份证
        self.idLabel.text = @"身份证正面(已认证)";
        self.idButton.enabled = NO;
        
        
        self.idsideLabel.text = @"身份证反面(已认证)";
        self.idsideButton.enabled = NO;
        
        
        self.cardLabel.text = @"银行卡正面(已认证)";
        self.cardButton.enabled = NO;
        
        
        self.holdIDLabel.text = @"手持身份证正面";
        self.holdIDButton.enabled = YES;

        self.holdCardLabel.text = @"手持银行卡正面";
        self.holdIDButton.enabled = YES;
        
        
    }else if (regStateNumber == 3){
        
        //身份证
        self.idLabel.text = @"身份证正面(已认证)";
        self.idButton.enabled = NO;
        
        
        self.idsideLabel.text = @"身份证反面(已认证)";
        self.idsideButton.enabled = NO;
        
        
        self.cardLabel.text = @"银行卡正面";
        self.cardButton.enabled = YES;


        self.holdIDLabel.text = @"手持身份证正面";
        self.holdIDButton.enabled = YES;

        self.holdCardLabel.text = @"手持银行卡正面";
        self.holdIDButton.enabled = YES;
    }else if (regStateNumber == 2){
        
        //身份证
        self.idLabel.text = @"身份证正面(已认证)";
        self.idButton.enabled = NO;
        
        
        self.idsideLabel.text = @"身份证反面";
        self.idsideButton.enabled = YES;


        self.cardLabel.text = @"银行卡正面";
        self.cardButton.enabled = YES;


        self.holdIDLabel.text = @"手持身份证正面";
        self.holdIDButton.enabled = YES;

        self.holdCardLabel.text = @"手持银行卡正面";
        self.holdIDButton.enabled = YES;
    }else{
        
        //身份证
        self.idLabel.text = @"身份证正面";
        self.idButton.enabled = YES;
        
        
        self.idsideLabel.text = @"身份证反面";
        self.idsideButton.enabled = YES;
        
        
        self.cardLabel.text = @"银行卡正面";
        self.cardButton.enabled = YES;
        
        
        self.holdIDLabel.text = @"手持身份证正面";
        self.holdIDButton.enabled = YES;
        
        self.holdCardLabel.text = @"手持银行卡正面";
        self.holdIDButton.enabled = YES;
    }
    
    
    //提交按钮
    UIButton * saveButton = [[UIButton alloc] init];
    saveButton.frame = CGRectMake(40 * px, CGRectGetMaxY(cardView.frame) + 20 * px, ScreenWidth - 80 * px, 150 * px);
    [saveButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    saveButton.layer.cornerRadius = 5;
    [saveButton addTarget:self action:@selector(saveButtonClick) forControlEvents:UIControlEventTouchUpInside];
    saveButton.backgroundColor = WYCmainColor;
    saveButton.titleLabel.font = [UIFont systemFontOfSize:MiddleFont];
    [saveButton setTitle:@"提交" forState:UIControlStateNormal];
    [self.view addSubview:saveButton];
    
}
#pragma make -------->>身份证正面
- (void)IDButtonClick{
    
    WYCLogFunc;
    
    WYCPhotographController * photo = [[WYCPhotographController alloc] init];
    photo.strType = @"1";
    [self presentViewController:photo animated:NO completion:nil];
}

#pragma mark --------->>身份证反面
- (void)sideButtonClick{
    if (regStateNumber == 1 || ![self.idLabel.text isEqualToString:@"身份证正面(已认证)"]) {
//        self.idButton.enabled = YES;
//        self.idsideButton.enabled = NO;
//        self.cardButton.enabled = NO;
//        self.holdCardButton.enabled = NO;
//        self.holdIDButton.enabled = NO;
        [self showToast:@"请按顺序点击"];
        return;
    }else{
        WYCPhotographController * photo = [[WYCPhotographController alloc] init];
        photo.strType = @"2";
        [self presentViewController:photo animated:NO completion:nil];
        
    }
    
}

#pragma mark --------->>银行卡
- (void)cardButtonClick{
    if (regStateNumber == 1 || regStateNumber == 2|| regStateNumber == 4 || regStateNumber == 5 || regStateNumber == 6  || ![self.idLabel.text isEqualToString:@"身份证正面(已认证)"] || ![self.idsideLabel.text isEqualToString:@"身份证反面(已认证)"]) {
//        self.idButton.enabled = NO;
//        self.idsideButton.enabled = YES;
//        self.cardButton.enabled = NO;
//        self.holdCardButton.enabled = NO;
//        self.holdIDButton.enabled = NO;
        [self showToast:@"请按顺序点击"];
        return;
    }else{
        WYCPhotographController * photo = [[WYCPhotographController alloc] init];
        photo.strType = @"3";
        [self presentViewController:photo animated:NO completion:nil];
    }
    
}

#pragma mark --------->>手持身份证
- (void)holdIdButtonClick{
    
    WYCLog(@"---------%d",regStateNumber);
    if (regStateNumber == 1 || regStateNumber == 2|| regStateNumber == 3 || regStateNumber == 5 || regStateNumber == 6 || ![self.idLabel.text isEqualToString:@"身份证正面(已认证)"] || ![self.idsideLabel.text isEqualToString:@"身份证反面(已认证)"] || ![self.cardLabel.text isEqualToString:@"银行卡正面(已认证)"]) {
//        self.idButton.enabled = NO;
//        self.idsideButton.enabled = NO;
//        self.cardButton.enabled = YES;
//        self.holdCardButton.enabled = NO;
//        self.holdIDButton.enabled = NO;
        [self showToast:@"请按顺序点击"];
        return;
    }else{
        WYCPhotographController * photo = [[WYCPhotographController alloc] init];
        photo.strType = @"4";
        [self presentViewController:photo animated:NO completion:nil];
    }
}




#pragma mark ------------->>手持银行卡
- (void)holdcardButtonClick{
    if (regStateNumber == 1 || regStateNumber == 2|| regStateNumber == 3 || regStateNumber == 4|| regStateNumber == 6 || ![self.idLabel.text isEqualToString:@"身份证正面(已认证)"] || ![self.idsideLabel.text isEqualToString:@"身份证反面(已认证)"] || ![self.cardLabel.text isEqualToString:@"银行卡正面(已认证)"] || ![self.holdIDLabel.text isEqualToString:@"手持身份证正面(已认证)"]) {

        [self showToast:@"请按顺序点击"];
        return;
    }else{
        WYCPhotographController * photo = [[WYCPhotographController alloc] init];
        photo.strType = @"5";
        [self presentViewController:photo animated:NO completion:nil];
    }
   
}

- (void)backClick{
    [self dismissViewControllerAnimated:NO completion:nil];
    
}
#pragma make ------->>提交按钮事件
- (void)saveButtonClick{
    if ([self.idLabel.text isEqualToString:@"身份证正面"] || [self.idsideLabel.text isEqualToString:@"身份证反面"] || [self.cardLabel.text isEqualToString:@"银行卡正面"] || [self.holdIDLabel.text isEqualToString:@"手持身份证正面"] || [self.holdCardLabel.text isEqualToString:@"手持银行卡正面"]) {
        [self showToast:@"请检查认证状态"];
        return;
    }
    WYCLogFunc;
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    params[@"userId"] = [WYCAccountTool unarchiveuserId];
    params[@"ticket"] = [WYCAccountTool unarchiveticket];
    [HttpTool POST:userauth params:params success:^(NSURLSessionDataTask *task, id responseObject) {
        WYCLog(@"responseObject状态是%@",responseObject);
        WYCLog(@"状态是%d",regStateNumber);
        if ([[responseObject objectForKey:@"code"] intValue] == 0) {
            [self showToast:msgStr];
            UIViewController *vc =self.presentingViewController;
            
            //ReadBookController要跳转的界面
            
            while (![vc isKindOfClass:[WYCLoginController class]]) {
                
                
                
                vc = vc.presentingViewController;
                
            }
            
            [vc dismissViewControllerAnimated:YES completion:nil];
            
        }else{
            regStateNumber = 1;
            
            //身份证
            self.idLabel.text = @"身份证正面";
            self.idButton.enabled = YES;
            
            
            self.idsideLabel.text = @"身份证反面";
            self.idsideButton.enabled = YES;
            
            
            self.cardLabel.text = @"银行卡正面";
            self.cardButton.enabled = YES;
            
            
            self.holdIDLabel.text = @"手持身份证正面";
            self.holdIDButton.enabled = YES;
            
            self.holdCardLabel.text = @"手持银行卡正面";
            self.holdIDButton.enabled = YES;
            
            
            [self showToast:msgStr];
            
        }
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        [self showToast:Failed];
    }];
    
}



@end
